home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / CH2 / Geometry.cs < prev    next >
Text File  |  2006-05-29  |  2KB  |  54 lines

  1. // ========================================================================
  2. //  Geometry.cs
  3. //
  4. //  This program adds calculates the distance around the perimiter of
  5. //  a quadrilateral, as well as the area of the quadrilateral and outputs the
  6. //  values. It recognizes whether the quadrilateral is a square or a rectangle
  7. //  modifies its output accordingly. Program assumes that all angles in the
  8. //  quadrilateral are equal. Demonstrates the if-else statement.
  9. // ========================================================================
  10.  
  11. function calcAndPrint(%theWidth, %theHeight)
  12. // ------------------------------------------------------------------------
  13. //    This function does the shape analysis and prints the result.
  14. //
  15. //    PARAMETERS: %theWidth - horizontal dimension
  16. //                %theHeight - vertical dimension
  17. //
  18. //    RETURNS: none
  19. // ------------------------------------------------------------------------
  20. {
  21.   // calculate perimeter
  22.   %perimeter = 2 * (%theWidth+%theHeight);
  23.  
  24.   // calculate area
  25.   %area = %theWidth * %theHeight;
  26.  
  27.   // first, setup the dimension output string
  28.   %prompt = "For a " @ %theWidth @ " by " @
  29.              %theHeight @ " quadrilateral, area and perimeter of ";
  30.  
  31.   // analyze the shape's dimensions and select different
  32.   // descripters based on the shape's dimensions
  33.   if (%theWidth == %theHeight)                // if true, then it's a square
  34.     %prompt = %prompt @ "square: ";
  35.   else                                        // otherwise it's a rectangle
  36.     %prompt = %prompt @ "rectangle: ";
  37.  
  38.   // always output the analysis
  39.   echo (%prompt @ %area @ " " @ %perimeter);
  40. }
  41.  
  42. function runGeometry()
  43. // ------------------------------------------------------------------------
  44. //     Entry point for the program.
  45. // ------------------------------------------------------------------------
  46. {
  47.  
  48.    // calculate and output the results for three
  49.    // known dimension sets
  50.    calcAndPrint(22, 26); // rectangle
  51.    calcAndPrint(31, 31); // square
  52.    calcAndPrint(47, 98); // rectangle
  53. }
  54.